﻿// See https://aka.ms/new-console-template for more information
using System.IO.Pipes;
using System.Text;

Console.WriteLine("Named Pipe Client is running...");
using (var client = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
{
    Console.WriteLine("等待连接到服务器");
    await client.ConnectAsync();
    Console.WriteLine("已连接到服务器");
    //发送消息
    string message = "你好，我是客户端";
    byte[] buffer = Encoding.UTF8.GetBytes(message);
    await client.WriteAsync(buffer, 0, buffer.Length);
    //读取响应
    byte[] responseBuffer = new byte[1024];
    int bytesRead = await client.ReadAsync(responseBuffer, 0, responseBuffer.Length);
    var response = Encoding.UTF8.GetString(responseBuffer, 0, bytesRead);
    Console.WriteLine("收到服务器响应" + response);
    Console.ReadLine();
}
